﻿using FlyIL.Trace;

namespace FlyIL.Optimizer
{
    class CallPass : SimplfyRoot
    {
        public override bool IsMatch(Context con, Inst node, ExpByTrace expression = null)
            => (node.Opt == Opt.call && !node.O1.IsImm);

        /// <summary>
        ///  call 的情况 
        /// </summary>
        public override void Do(Context self, Inst node, ExpByTrace expression = null)
        {

            var pre = self.GetPreviousIfExist(node);

            var inst = node;
            var nnn = Tracer.TraceWeak(self, pre, ref inst.O1);

            if (nnn != null && nnn.IsValid)
            {
                if (nnn.Calculable)
                {

                    byte index = node.O1.u8;
                    if (self.Is64)
                    {


                        node.jmp(nnn.Result);
                        self.InsertBefore(node).make(Opt.push, inst.Vip + (uint)inst.O3);
                    }
                    else
                    {
                        node.jmp((uint)nnn.Result);
                        self.InsertBefore(node).make(Opt.push, inst.Vip + (ulong)inst.O3);

                    }
                }
            }

        }


    }
}
